package handler

import (
	"context"
	"encoding/json"
	"fmt"
	"google.golang.org/protobuf/types/known/emptypb"
	"rpc_demo/goods_srv/global"
	"rpc_demo/goods_srv/model"
	"rpc_demo/goods_srv/proto"
)

type CategorySrv struct {
}

func (c *CategorySrv) GetAllCategoryList(ctx context.Context, req *emptypb.Empty) (*proto.CategoryListResponse, error) {
	var categorys []model.Category

	global.Db.Where(&model.Category{Level: 1}).Preload("SubCategory.SubCategory").Find(&categorys)
	var totoal int64
	global.Db.Where(&model.Category{Level: 1}).Preload("SubCategory.SubCategory").First(&categorys)

	fmt.Println("total", totoal)
	marshal, err := json.Marshal(&categorys)
	if err != nil {
		return nil, err
	}

	return &proto.CategoryListResponse{
		JsonData: string(marshal),
	}, nil
}

// 获取子分类
func (c *CategorySrv) GetSubCategory(ctx context.Context, req *proto.CategoryListRequest) (*proto.SubCategoryListResponse, error) {
	return &proto.SubCategoryListResponse{}, nil
}
func (c *CategorySrv) DeleteCategory(ctx context.Context, req *proto.DeleteCategoryRequest) (*emptypb.Empty, error) {
	return &emptypb.Empty{}, nil
}
func (c *CategorySrv) UpdateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*emptypb.Empty, error) {
	return &emptypb.Empty{}, nil
}
func (c *CategorySrv) CreateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.CategoryInfoResponse, error) {
	return &proto.CategoryInfoResponse{}, nil
}
